package com.bdqn.servlet;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bdqn.base.R;
import com.bdqn.entity.Book;
import com.bdqn.service.BookService;
import com.bdqn.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * BookServlet
 *
 * @author LILIBO
 * @since 2024/5/24
 */
@WebServlet("/api/book")
public class BookServlet extends HttpServlet {

    // 引入业务逻辑层
    private final BookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // String name = request.getParameter("name");
        // System.out.println("请求参数：name=" + name);

        // 获取JSON格式

        InputStream inputStream = request.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder jsonStr = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonStr.append(line);
        }
        String jsonBody = jsonStr.toString();
        System.out.println("请求: " + jsonBody);
        JSONObject json = JSONObject.parseObject(jsonBody);
        String op = (String) json.get("op"); // {"op": "list"}

        // String op = request.getParameter("op");
        PrintWriter out = response.getWriter();
        R result = null;
        switch (op) {
            case "add": // 新增图书
                Book addBook = new Book();
                result = add(addBook);
                break;
            case "upd": // 修改图书
                Book updBook = new Book();
                result = upd(updBook);
                break;
            case "del": // 删除图书
                String id = request.getParameter("id");
                result = del(id);
                break;
            case "list": // 查询图书列表
                result = list();
                break;
            default:
                result = R.failure("接口未定义");
                break;
        }
        String jsonObj = JSON.toJSONString(result);
        System.out.println("响应: " + jsonObj);
        out.print(jsonObj); // 将接口处理结果响应给客户端
        out.flush();
        out.close();
    }

    /**
     * 添加图书
     *
     * @param book 图书对象
     * @return 响应内容
     */
    private R add(Book book) {
        int add = bookService.add(book);
        return add == 1 ? R.success() : R.failure(); // {"code":0,"count":0,"data":null,"msg":"success"}
    }

    private R upd(Book updBook) {
        int upd = bookService.upd(updBook);
        return upd == 1 ? R.success() : R.failure(); // {"code":0,"count":0,"data":null,"msg":"success"}
    }

    private R del(String id) {
        int del = bookService.del(Integer.parseInt(id));
        return del == 1 ? R.success() : R.failure(); // {"code":0,"count":0,"data":null,"msg":"success"}
    }

    private R list() {
        List<Book> list = bookService.list();
        return R.success(list); // {"code":0,"count":2,"data":[{"address":"高新区","createTime":"2024-02-01","id":11,"linkman":"张通通","mobile":"15900000002","modifyTime":"2024-02-24 02:16:17","remark":"","status":0,"type":0,"wechat":"通通"},{"address":"湖南省郴州市第一职中内郴州科泰","createTime":"2024-02-01","id":45,"linkman":"张通通","mobile":"15900000005","modifyTime":"2024-02-24 00:00:00","remark":"","status":0,"type":1,"wechat":""}],"msg":"success"}
    }

}
